/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Shop                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Services                                  
*│　类    名： ShopService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.Common;
using NetCoreCMS.IRepository;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetCoreCMS.Services
{
    public class ShopService : IShopService
    {
        private readonly IShopRepository _repository;
        private readonly IShop_albumsRepository _albums;

        public ShopService(IShopRepository repository, IShop_albumsRepository albums)
        {
            _repository = repository;
            _albums = albums;
        }

        public Shop GetModel(int id)
        {
            var model = _repository.Get(id);
            if(model!=null)
            model.albums = _albums.GetList("where shop_id=@id", new { id = id }).ToList();
            return model;
        }
        public List<Models.Shop> GetList(int pageNumber, int rowsPerPage, string conditions, string orderby, object parameters = null)
        {
            return _repository.GetListPaged(pageNumber, rowsPerPage, conditions, orderby, parameters).ToList();
        }
        public int RecordCount(string conditions, object parameters = null)
        {
            return _repository.RecordCount(conditions, parameters);
        }
        public bool Delete(int id)
        {
            return _repository.Delete(id) > 0;
        }

        public Shop GetFirstByUserId(int user_id)
        {
            return _repository.GetList("where user_id=" + user_id).FirstOrDefault();
        }

        public bool UpdateField(int id, string strValue)
        {
            return _repository.UpdateField(id, strValue);
        }

        public bool Exists(int id)
        {
            return GetModel(id) != null ? true : false;
        }
        /// <summary>
        /// 返回信息内容
        /// </summary>
        public string GetContent(int shop_id)
        {
            string content = GetModel(shop_id).remark;
            if (string.IsNullOrEmpty(content))
            {
                return string.Empty;
            }
            return content;
        }
        public bool Delete(int id, Models.Sysconfig sysConfig, string env)
        {
            string content = GetContent(id);//获取信息内容
            bool result = _repository.DeleteAll(id, env);//删除内容
            if (result && !string.IsNullOrEmpty(content))
            {
                FileHelper.DeleteContentPic(content, "/" + sysConfig.filepath, env); //删除内容图片
            }
            return result;
        }
        public bool Update(Shop model, string env)
        {
            try
            {
                #region 修改主表数据==========================
                _repository.Update(model);
                #endregion
                #region 修改图片相册==========================
                //删除/添加/修改相册图片
                _albums.Update(model.albums, model.id, env);
                #endregion
                return true;
            }
            catch
            {
                return false;
            }
        }

        public int Add(Shop model)
        {
            int id = (int)_repository.Insert(model);

            //albums
            if (model.albums != null)
                for (int i = 0; i < model.albums.Count; i++)
                {
                    model.albums[i].shop_id = id;
                    _albums.Insert(model.albums[i]);
                }
            return id;
        }
    }
}